//
// Copyright (c) 2005 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

package com.broadvision.manage.common.tools.action.user;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.Globals;

import com.broadvision.data.common.DataConstants;
import com.broadvision.portal.services.UserTemplate;
import com.broadvision.manage.tools.action.BaseAction;
import com.broadvision.portal.services.util.TransferBeanUtils;
import com.broadvision.web.util.ViewDynaBean;
import com.broadvision.manage.common.tools.user.UserProfile;
import com.broadvision.system.accesscontrol.client.*;
import com.broadvision.visitor.client.VisitorManager;
import com.broadvision.web.action.Tab;
import com.broadvision.web.servlet.SessionVisitor;
import com.broadvision.visitor.client.Visitor;
import com.broadvision.license2.LicUtil;
import com.broadvision.lictool2.LicToolWebApp;

/**
 * Creates a new user in the BVMC.
 */
public class ProfileNewAction extends BaseAction {

  public static final String VIEW_BEAN_NAME = "mtUserEditView";

  /**
   * Implements the action for creating a new user.
   * <p> <p>
   * @param mapping <code>ActionMapping</code> object used to select this instance
   * @param form Optional <code>ActionForm</code> bean for this request (if any)
   * @param request <code>HttpServletRequest</code> object to process
   * @param response <code>HttpServletResponse</code> object to process
   * <p> <p>
   * @return <code>ActionForward</code> instance describing where and how
   * control should be forwarded, or null if the response has already been completed
   * <p> <p>
   * @throws Exception if an error occurs
   */
  public ActionForward processExecute(ActionMapping mapping,
				      ActionForm form,
				      HttpServletRequest request,
				      HttpServletResponse response)
    throws Exception 
  {

    try {
      Visitor visitor = getVisitor(request);

      if (!UserProfile.isPermitted(visitor, "Edit") && (!UserProfile.isOrganizationAdmin(visitor))) {
	  request.setAttribute(Globals.ERROR_KEY, "error.user.profile.no.edit");
	  return mapping.findForward("failure");
      }

      UserProfile userProfile = new UserProfile();
      userProfile.reset();
      
      TransferBeanUtils.copyPropertiesToView(form, userProfile);
      UserUtils.setP(form, "cancelAction", "/mt/userSearch.do");

      ViewDynaBean view = new ViewDynaBean(VIEW_BEAN_NAME, mapping);
      request.setAttribute(VIEW_BEAN_NAME, view);
      
      TransferBeanUtils.copyPropertiesToView(view, userProfile);
      TransferBeanUtils.copyPropertiesToView(view, userProfile.getDynaBean());
      UserUtils.setP(view, "useFullName", new Boolean(!VisitorManager.isUsingFMLName()));

      // grab the list of user templates: a temporary solution before the pop-up page is ready
      List userTemplateList = UserTemplate.getUserTemplates (getServiceId(request),
							     DataConstants.BV_ONLINE_DB_CONTENT,
							     0,
							     getPortalVisitor(request));


 
      int retcode =  LicToolWebApp.getInstance().checkTag("portal.framework.basic");
      UserUtils.setP(view, "showUserTemplate", new Boolean((retcode >= 0)));
      UserUtils.setP(view, "showUserGroup", new Boolean((retcode >= 0)));
      UserUtils.setP(view, "isUserAdmin", new Boolean(isUserInRole(visitor, "UserAdmin")));

      UserUtils.setP(view, "editable", UserProfile.isPermitted(visitor, "Edit") ? Boolean.TRUE : Boolean.FALSE);

      UserUtils.setP(view, "userTemplateList", userTemplateList);
      
	  //
	  UserUtils.setP(view, "userDataList", VisitorDataUtilities.getUserListData());
      UserUtils.setP(view, "userTypeList", VisitorDataUtilities.getUserTypesData());
      UserUtils.setP(view, "corporateStatusList", VisitorDataUtilities.getCorporateUserStatusData());
      //
    }
    catch (Exception ex) {
	request.setAttribute(Globals.ERROR_KEY, "error.application");
	return mapping.findForward("failure");
    }
    return mapping.findForward("success");
  }
}





